package com.fengze.shop.common.listener;

import com.fengze.shop.common.cache.DCManager;
import com.fengze.shop.common.config.ServerInfo;
import com.fengze.shop.common.config.SystemConfig;
import com.fengze.shop.common.utils.Constant;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Timer;

/**
 * 初始化本类 - 系统启动配置类
 *
 * Created by zhangls on 2016/11/22 0022.
 */
@Slf4j
public class InitContextListener implements ServletContextListener {

    private ServletContext context = null;

//    Timer lockTimer;// 删除锁定用户的时间器

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.context = event.getServletContext();

//        lockTimer = new Timer();

        //设置系统名称
        ServerInfo.setSystemName(Constant.SYS_FILE_NAME);

        String configPath = ServerInfo.getSystemRootPath() + ServerInfo.FILE_SEP;

        log.info("[重要] 系统配置文件路径 configPath : {}", configPath);

        ServerInfo.setSystemConfigFile(configPath + Constant.CONFIG_FILE_NAME);

        // 加载系统配置模块
        log.info("[重要] Start load [SystemConfig.xml] Begin! ");
        systemConfigInit(ServerInfo.getSystemConfigFile());
        log.info("[重要] Load SUCCESS [SystemConfig.xml] OK! ");

        log.info("[重要] Start load [DataCache model] Begin! ");
        DCManager.getInstance();
        log.info("[重要] Load SUCCESS [DataCache model] OK! ");
    }

    /**
     * 系统配置模块加载
     * @param configFile
     */
    private void systemConfigInit(String configFile) {
        SystemConfig.getSystemConfig().setConfigFile(configFile);
        SystemConfig.getSystemConfig().load();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
//        lockTimer = new Timer();
    }
}